package org.cuit.echo.seon.subject;

import org.cuit.echo.seon.observe.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Seon
 * @version 1.0
 * @since 2024-09-10
 */
public class WeatherStation implements Subject {

    private final List<Observer> observers;
    private float temperature, humidity;

    public WeatherStation() {
        this.observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        observers.forEach(item -> item.update(temperature, humidity));
    }

    /**
     * 当天气数据更新的时候就调用这个方法
     */
    public void setWeatherData(float temperature, float humidity) {
        this.temperature = temperature;
        this.humidity = humidity;
        notifyObservers();
    }
}
